CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
PROJECT(compiler_options)

ADD_LIBRARY(test test.cpp)
# 为test增加预编译选项
TARGET_COMPILE_DEFINITIONS(test
                            PRIVATE PRIVATE_PRINT
                            PUBLIC PUBLIC_PRINT
                            INTERFACE INTERFACE_TEST_DEF)
# 打印test的属性
GET_PROPERTY(defs TARGET test PROPERTY COMPILE_DEFINITIONS)
MESSAGE("test COMPILE_DEFINITIONS: ${defs}")
GET_PROPERTY(interface_defs TARGET test PROPERTY INTERFACE_COMPILE_DEFINITIONS)
MESSAGE("test COMPILE_DEFINITIONS: ${interface_defs}")

ADD_EXECUTABLE(main main.cpp)
TARGET_COMPILE_DEFINITIONS(main PUBLIC DEBUG_PRINT)
# 打印main的属性
GET_PROPERTY(defs TARGET main PROPERTY COMPILE_DEFINITIONS)
MESSAGE("main COMPILE_DEFINITIONS: ${defs}")
GET_PROPERTY(interface_defs TARGET main PROPERTY INTERFACE_COMPILE_DEFINITIONS)
MESSAGE("main COMPILE_DEFINITIONS: ${interface_defs}")

TARGET_LINK_LIBRARIES(main test)
# 链接了test库之后，再次打印main的属性
GET_PROPERTY(defs TARGET main PROPERTY COMPILE_DEFINITIONS)
MESSAGE("after link, main COMPILE_DEFINITIONS: ${defs}")
GET_PROPERTY(interface_defs TARGET main PROPERTY INTERFACE_COMPILE_DEFINITIONS)
MESSAGE("after link, main COMPILE_DEFINITIONS: ${interface_defs}")